package com.hod.client.chargedescription;

import com.google.gwt.user.client.Window;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.TextField;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataObject;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.frontend.HODForm;

public class ChargeDescriptionFormPanel extends HODForm {
	
	protected final TextField txtIdChargeDescription = new TextField("idChargeDescription", "idChargeDescription", 150);
    protected final TextField txtShortDescription = new TextField("shortDescription", "shortDescription", 150);
    protected final TextField txtLongDescription = new TextField("longDescription", "longDescription", 150);


	public ChargeDescriptionFormPanel(GearsDataStore ds) {
		super(ds);
		// TODO Auto-generated constructor stub
		add(createFieldSet());        
        createButtons();
	}

	@Override
	protected void createButtons() {
		// TODO Auto-generated method stub
		ButtonListenerAdapter savelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		ChargeDescription o = null;
            		if (txtIdChargeDescription.getText()==""){
            			o = new ChargeDescription();
            			o.setIdChargeDescription(null);
            		}else{
            			o = (ChargeDescription) ds.findById(Integer.valueOf(txtIdChargeDescription.getText()));
            		}
                    o.setShortDescription(txtShortDescription.getText());
                    o.setLongDescription(txtLongDescription.getText());
                    ds.save(o);
                    HOD.reload();
				} 
            	catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        ButtonListenerAdapter deletelistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		ds.delete((ChargeDescription) ds.findById(Integer.valueOf(txtIdChargeDescription.getText())));
            		HOD.reload();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        ButtonListenerAdapter createlistener = new ButtonListenerAdapter() {
            public void onClick(Button button, EventObject e) {
            	try {
            		getForm().reset();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					Window.alert(e1.getMessage());
				}
            }
        };
        	           
        
        Button save = new Button("Save", savelistener);
        Button delete = new Button("Delete", deletelistener);
		Button newRecord = new Button("New", createlistener);
        Button cancel = new Button("Cancel");

        addButton(save);
        addButton(delete);
        addButton(cancel);
        addButton(newRecord);

	}

	@Override
	protected FieldSet createFieldSet() {
		// TODO Auto-generated method stub
		txtIdChargeDescription.setReadOnly(true);
		txtIdChargeDescription.setVisible(false);
		txtIdChargeDescription.setHideLabel(true);
		txtShortDescription.setAllowBlank(false);
		txtLongDescription.setAllowBlank(false);
		
		FieldSet fieldSet = new FieldSet();
        fieldSet.setLabelWidth(80);
        fieldSet.setTitle("Charge description details");
        fieldSet.setAutoHeight(true);
        fieldSet.setBorder(false);

        //the field names must match the data field values from the Store
        fieldSet.add(txtIdChargeDescription);
        fieldSet.add(txtShortDescription);
        fieldSet.add(txtLongDescription);
        return fieldSet;
	}

	@Override
	public void updateDataControls(Record record) {
		// TODO Auto-generated method stub
		getForm().loadRecord(record);  

	}

	@Override
	public void setObject(GearsDataObject object) {
		// TODO Auto-generated method stub
		
	}

}
